import rb, rhythmdb
import pygtk, gtk, gtk.glade
import gobject
import gconf

from VideoSource import VideoSource
from VideoSourceConfigDlg import VideoSourceConfigDlg
class VideoEntryType(rhythmdb.EntryType):
	def __init__(self):
		rhythmdb.EntryType.__init__(self, name='magnatune')
		self.category = rhythmdb.ENTRY_STREAM
	def can_sync_metadata(self, entry):
		return True

class VideoLibPlugin(rb.Plugin):
	def dialog_response (self, dialog, response):
		dialog.hide()
		gladexml = gtk.glade.XML(self.find_file("videosource-prefs.glade"))
		current_folder = gladexml.get_widget('file_path_entry').get_current_folder()
		self.mysource.setLibrary(current_folder)
		print current_folder
	def create_configure_dialog(self,dialog = None):
		if not dialog:
			glade_file = self.find_file("videosource-prefs.glade")
			dialog = VideoSourceConfigDlg(glade_file).get_dialog()
		dialog.present()	
		return dialog
	def __init__(self):
		rb.Plugin.__init__(self)
		self.gconf = gconf.client_get_default()
		self.gladexml = None 

	def activate(self, shell):
		data = {}
		self.shell = shell

		self.db = shell.get_property("db")
		self.entry_type = VideoEntryType()
		self.db.register_entry_type(self.entry_type)
		#the icon
		theme = gtk.icon_theme_get_default()
		#rb.append_plugin_source_path(theme, "/icons")
		
		width, height = gtk.icon_size_lookup(gtk.ICON_SIZE_LARGE_TOOLBAR)
		icon = rb.try_load_icon(theme, "video", width, 0)
		#width, height = gtk.icon_size_lookup(gtk.ICON_SIZE_LARGE_TOOLBAR)
		#icon = gtk.gdk.pixbuf_new_from_file_at_size(self.find_file("video.svg"), width, height)
		group = rb.rb_source_group_get_by_name('library')
		self.mysource = gobject.new (VideoSource, shell=shell, name=_("Video"), entry_type=self.entry_type,icon=icon,db=self.db, plugin=self,source_group=group)
		#add the source and the entry type
		shell.register_entry_type_for_source(self.mysource, self.entry_type)
		shell.append_source(self.mysource, None)
		
		self.pec_id = shell.get_player().connect_after('playing-song-changed', self.playing_entry_changed)
		self.pec_id2 = shell.get_player().connect_after('playing-changed', self.playing_changed)
		#self.mysource.do_impl_activate()
	def deactivate(self, shell):
		sp = self.shell.get_player ()
		self.shell.get_player().disconnect (self.pec_id)
		self.shell.get_player().disconnect (self.pec_id2)
		self.pec_id = None
		self.pec_id2 = None
		self.db.entry_delete_by_type(self.entry_type)
		self.db.commit()
		
		self.mysource.delete_thyself()
		self.mysource = None
		self.shell = None
		self.gconf_keys  = None
		self.db = None
		
	def playing_entry_changed(self,sp, entry):
		self.mysource.playing_entry_changed (entry)
	def playing_changed(self,sp,playing):
		self.mysource.playing_changed (playing)
	#def tocurrrent(self,data = None):
	#	self.shell.get_player().jump_to_current()

